package nccloud.mob.util.erm.webpub;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import nc.itf.cmp.settlement.ISettlementQueryService;
import nc.itf.obm.ebank.IObmEbankNccYhhdService;
import nc.vo.cmp.settlement.SettlementAggVO;
import nc.vo.cmp.settlement.SettlementBodyVO;
import nc.vo.obm.cjzfdzhd.YhhdVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.filesystem.NCFileVO;
import nccloud.framework.core.exception.ExceptionUtils;
import nccloud.framework.mob.container.MobClientInfo;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.container.ClientInfo;
import nccloud.framework.web.container.SessionContext;
import nccloud.pubitf.erm.expenseaccount.IWebPubService;
import nccloud.pubitf.platform.attachment.IAttachmentService;
import nccloud.pubitf.platform.attachment.IFileStorageConst;
import nccloud.pubitf.platform.attachment.WebFileParaVO;
import nccloud.web.platform.attachment.action.WebFileParaConvertor;

public class AttchmentQueryUtil {
	public static List<WebFileParaVO> yhhdEnclosureQuery(String[] transerials) {
		IObmEbankNccYhhdService obmEbankNccYhhdService = ServiceLocator
				.find(IObmEbankNccYhhdService.class);

		IAttachmentService ncservice = ServiceLocator.find(IAttachmentService.class);

		String userid = SessionContext.getInstance().getClientInfo().getUserid();

		List<WebFileParaVO> webFileParaVOList = new ArrayList();

		YhhdVO[] yhhdvos = null;

		try {
			yhhdvos = obmEbankNccYhhdService.queryEbankYhhdByBankSeqNos(transerials);

		} catch (BusinessException var11) {
			ExceptionUtils.wrapException(var11);
		}
		if (yhhdvos == null) {
			return webFileParaVOList;

		} else {
			String[] pkyhhds = new String[yhhdvos.length];
			for (int i = 0; i < yhhdvos.length; ++i) {
				pkyhhds[i] = yhhdvos[i].getPk_yhhd();
				NCFileVO[] ncfiles = ncservice.queryNCFilesByNodePath(yhhdvos[i].getPk_yhhd(), userid);

				WebFileParaConvertor convertor = new WebFileParaConvertor();
				WebFileParaVO[] ret = convertor.convert(ncfiles, yhhdvos[i].getPk_yhhd(), IFileStorageConst.Bucket);

				webFileParaVOList.addAll(Arrays.asList(ret));
			}
			return webFileParaVOList;
		}
	}

	public static List<WebFileParaVO> getYhhdByBillid(String billid) {
		ClientInfo info = SessionContext.getInstance().getClientInfo();
		MobClientInfo mobClientInfo = new MobClientInfo();
		String pk_group = info != null ? info.getPk_group() : mobClientInfo.getPk_group();

		boolean flag = ServiceLocator.find(IWebPubService.class).isProductInstalled(pk_group,
				"3610");

		if (!flag) {
			return null;
		} else {
			ArrayList tfblists = new ArrayList();
			try {
				SettlementAggVO settlementVO = ServiceLocator
						.find(ISettlementQueryService.class).querySettlementAggByPk_busibill(billid);

				if (settlementVO != null) {

					SettlementBodyVO[] setlementbodyvos = ((SettlementBodyVO[]) settlementVO
							.getChildrenVO());
					if (setlementbodyvos != null && setlementbodyvos.length != 0) {
						SettlementBodyVO[] var5 = setlementbodyvos;
						int var6 = setlementbodyvos.length;
						for (int var7 = 0; var7 < var6; ++var7) {
							SettlementBodyVO settlebody = var5[var7];
							if (settlebody.getTranserial() != null) {
								tfblists.add(settlebody.getTranserial());
							}
						}
					}
				}
			} catch (BusinessException var9) {
				;
			}

			String[] transerials = (String[]) tfblists.toArray(new String[tfblists.size()]);
			List<WebFileParaVO> webFileParaVOs = new ArrayList();

			if (transerials.length > 0) {
				webFileParaVOs = yhhdEnclosureQuery(transerials);
			}
			return webFileParaVOs;
		}
	}

	public static WebFileParaVO[] billEnclosureQuery(String fullPath, String billid) {
		IAttachmentService ncservice = ServiceLocator.find(IAttachmentService.class);
		// liyingtj
		ClientInfo info = SessionContext.getInstance().getClientInfo();
		MobClientInfo mobClientInfo = new MobClientInfo();
		String userid = info != null ? info.getUserid() : mobClientInfo.getUserid();
		//      String userid = SessionContext.getInstance().getClientInfo().getUserid();

		NCFileVO[] ncfiles = ncservice.queryNCFilesByNodePath(fullPath, userid);
		WebFileParaConvertor convertor = new WebFileParaConvertor();
		WebFileParaVO[] webFileParaVOs = convertor.convert(ncfiles, billid, IFileStorageConst.Bucket);

		return webFileParaVOs;
	}
}
